﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Framework.Service;
using FantastPlatform.Framework;
using NHibernate;
using NHibernate.Criterion;
using FantastPlatform.Decision.IModel;
using FantastPlatform.Decision.Model;
using FantastPlatform.Decision.IService;

namespace FantastPlatform.Decision.Service
{
    public class FlowImplementService : DataService<IFlowImplement, FlowImplement>, IFlowImplementService
    {

        #region IFlowImplementService 成员

        public IFlowImplement Implement(IFlowLevel flowLevel)
        {
            try
            {
                IFlowImplement flowImplement = null;
                if (flowLevel != null)
                {
                    if (flowLevel.FlowFile != null)
                    {

                        IFlowImplementService flowImplementService = ServiceFactory.CreateService<IFlowImplementService>();
                        flowImplement = new FlowImplement();
                        flowImplement.FlowLevel = flowLevel;
                        flowImplement.StartTime = DateTime.Now;
                        flowImplement.Status = "执行中";
                        flowImplement.TotalPerson = flowLevel.PersonNumber;
                        flowImplement.Level = flowLevel.Level;
                        flowImplementService.Create(flowImplement);
                        IList<IFlowElement> list = (from f in flowLevel.FlowFile.FlowElements
                                                    where f.Level == flowLevel.Level
                                                    select f).ToList();
                        foreach (FlowElement item in list)
                        {
                            IElementImplementService elementImplementServcie = ServiceFactory.CreateService<IElementImplementService>();

                            IElementImplement elementImplement = new ElementImplement()
                            {
                                FlowImplement = flowImplement,
                                Details = item.Details,
                                Equip = item.Equip,
                                EchelonNumber = item.EchelonNumber,
                                Organization = item.Organization,
                                IsPersonArrive = false,
                                 StatusInfo="未开始执行",
                                IsTaskOver = false,
                                Require = item.Require,
                                PersonNumber = item.PersonNumber
                            };
                            elementImplementServcie.Create(elementImplement);
                        }

                    }
                }
                return flowImplement;
            }
            catch (Exception)
            {
                return null;
            }

        }

        #endregion
    }
}
